home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / CD32 / CD32_Support / examples / SA_Examples / lowlevel / KBInt / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-31  |  5.9 KB  |  306 lines

  1. /****** CDGS/KBInt **********************************************************
  2. *
  3. *   NAME
  4. *       KBInt -- test program for lowlevel.library/AddKBInt(),
  5. *                lowlevel.library/RemKBInt(), and lowlevel.library/GetKey()
  6. *
  7. *   SYNOPSIS
  8. *       KBInt
  9. *
  10. *   FUNCTION
  11. *       Test program for lowlevel.library/AddKBInt(), lowlevel.library/RemKBInt(),
  12. *       and lowlevel.library/GetKey().
  13. *
  14. *       Sets up keyboard interrupt handler and displays all keyboard
  15. *       activity.
  16. *
  17. *       To terminate this program, select the close gadget.
  18. *
  19. *   INPUTS
  20. *       None
  21. *
  22. *   RESULT
  23. *       RETURN_OK (0)       -   success
  24. *       RETURN_WARN (5)     -   warning
  25. *       RETURN_ERROR (10)   -   error
  26. *       RETURN_FAIL (20)    -   failure
  27. *
  28. *   EXAMPLE
  29. *
  30. *   NOTES
  31. *
  32. *   BUGS
  33. *       If keyboard interrupts occur faster than keyboard update
  34. *       routine can display changes, some keyboard events will not
  35. *       be displayed.
  36. *
  37. *       Hopefully very few.
  38. *
  39. *   SEE ALSO
  40. *
  41. ******************************************************************************
  42. *
  43. */
  44.  
  45. /*
  46.  * System includes
  47.  */
  48.  
  49. #include <exec/types.h>
  50. #include <exec/memory.h>
  51.  
  52. #include <dos/dos.h>
  53.  
  54. #include <intuition/intuition.h>
  55. #include <intuition/gadgetclass.h>
  56.  
  57. #include <libraries/gadtools.h>
  58.  
  59. #include <devices/inputevent.h>
  60.  
  61. #include <libraries/lowlevel.h>
  62.  
  63. #include <clib/exec_protos.h>
  64. #include <clib/dos_protos.h>
  65. #include <clib/intuition_protos.h>
  66. #include <clib/gadtools_protos.h>
  67. #include <clib/keymap_protos.h>
  68. #include <clib/lowlevel_protos.h>
  69.  
  70. #include <pragmas/lowlevel_pragmas.h>
  71.  
  72. /*
  73.  * ANSI includes
  74.  */
  75.  
  76. #include <stdio.h>
  77. #include <stdlib.h>
  78. #include <string.h>
  79.  
  80. /*
  81.  * Local includes
  82.  */
  83.  
  84. #define MAIN
  85.  
  86. #include "kbint.h"
  87.  
  88. /****** KBInt/main ******************************************
  89. *
  90. *   NAME
  91. *       main -- main entry point
  92. *
  93. *   SYNOPSIS
  94. *       main(argc,argv);
  95. *
  96. *       void main(int argc,char *argv[]);
  97. *
  98. *   FUNCTION
  99. *       Main entry point.
  100. *
  101. *   INPUTS
  102. *       argc -- argument count
  103. *       argv -- argument value arrraay
  104. *
  105. *   RESULT
  106. *       None
  107. *
  108. *   EXAMPLE
  109. *
  110. *   NOTES
  111. *
  112. *   BUGS
  113. *       If there are any, you can get to 'em from here!
  114. *
  115. *   SEE ALSO
  116. *       shutdown()
  117. *
  118. ******************************************************************************
  119. *
  120. */
  121. void main(int argc,char *argv[])
  122. {
  123.  
  124.     /*
  125.      * Open libraries
  126.      */
  127.  
  128.     /* Open intuition.library */
  129.     IntuitionBase=OpenLibrary("intuition.library",KICKSTART_VERSION);
  130.     if (!IntuitionBase) {
  131.         Printf("%s: Error opening intuition.library V%ld\n",
  132.             PROGRAM_NAME,KICKSTART_VERSION);
  133.         goodbye(RETURN_FAIL);
  134.     }
  135.  
  136.     /* Open gadtool.library */
  137.     GadtoolsBase=OpenLibrary("gadtools.library",KICKSTART_VERSION);
  138.     if (!GadtoolsBase) {
  139.         Printf("%s: Error opening gadtools.library V%ld\n",
  140.             PROGRAM_NAME,KICKSTART_VERSION);
  141.         goodbye(RETURN_FAIL);
  142.     }
  143.  
  144.     /* Open keymap.library */
  145.     KeymapBase=OpenLibrary("keymap.library",KEYMAP_VERSION);
  146.     if (!KeymapBase) {
  147.         Printf("%s: Error opening keymap.library V%ld\n",
  148.             PROGRAM_NAME,KICKSTART_VERSION);
  149.         goodbye(RETURN_FAIL);
  150.     }
  151.  
  152.     /* Open lowlevel.library */
  153.     LowLevelBase=OpenLibrary("lowlevel.library",KICKSTART_VERSION);
  154.     if (!LowLevelBase) {
  155.         Printf("%s: Error opening lowlevel.library V%d\n",
  156.             PROGRAM_NAME,KICKSTART_VERSION);
  157.         goodbye(RETURN_FAIL);
  158.     }
  159.  
  160.     /*
  161.      * Set-up keyboard interrupt handler
  162.      */
  163.  
  164.     /* Allocate keyboard signal */
  165.     kbSignal=AllocSignal(-1);
  166.     if (!kbSignal) {
  167.         Printf("%s: Error allocating keyboard signal\n",PROGRAM_NAME);
  168.         goodbye(RETURN_FAIL);
  169.     }
  170.  
  171.     /* Find this task */
  172.     kbIntTask=FindTask(NULL);
  173.     if (!kbIntTask) {
  174.         Printf("%s: Error finding KBInt task\n",PROGRAM_NAME);
  175.         goodbye(RETURN_FAIL);
  176.     }
  177.  
  178.     /* Add keyboard interrupt handler */
  179.     kbIntHandle=AddKBInt(kbInterrupt,(APTR) KBINT_COOKIE);
  180.     if (!kbIntHandle) {
  181.         Printf("%s: Error installing keyboard interrupt handler\n",PROGRAM_NAME);
  182.         goodbye(RETURN_FAIL);
  183.     }
  184. #ifdef DEBUG
  185.     Printf("%s: Added keyboard interrupt handler $%08lx\n",
  186.         PROGRAM_NAME,kbIntHandle);
  187. #endif /* DEBUG */
  188.  
  189.     /*
  190.      * GUI
  191.      */
  192.  
  193.     /* Open GUI */
  194.     if (!guiOpen()) {
  195.         Printf("%s: Error opening user interface\n",PROGRAM_NAME);
  196.         goodbye(RETURN_FAIL);
  197.     }
  198.  
  199.     /* Process GUI */
  200.     guiLoop();
  201.  
  202.     /*
  203.      * Termination
  204.      */
  205.  
  206.     /* If any bad data to keyboard interrupt handler ... */
  207.     if (kbIntBadData) {
  208.         /* Warning */
  209.         Printf("%s: %lu calls to keyboard interrupt handler with bad data\n",
  210.             PROGRAM_NAME,kbIntBadData);
  211.     }
  212.  
  213.     /* Exit */
  214.     goodbye(kbIntBadData?RETURN_WARN:RETURN_OK);
  215.  
  216. }
  217.  
  218. /****** KBInt/goodbye ******************************************
  219. *
  220. *   NAME
  221. *       goodbye -- terminate program
  222. *
  223. *   SYNOPSIS
  224. *       goodbye(returnCode);
  225. *
  226. *       void goodbye(int returnCode);
  227. *
  228. *   FUNCTION
  229. *       Terminate program.
  230. *
  231. *   INPUTS
  232. *       returnCode -- return code (from dos/dos.h RETURN_*)
  233. *
  234. *   RESULT
  235. *       None
  236. *
  237. *   EXAMPLE
  238. *
  239. *   NOTES
  240. *
  241. *   BUGS
  242. *
  243. *   SEE ALSO
  244. *       main()
  245. *
  246. ******************************************************************************
  247. *
  248. */
  249. void goodbye(int returnCode)
  250. {
  251.  
  252.     /*
  253.      * Remove keyboard interrupt handler
  254.      */
  255.  
  256.     /* Free keyboard signal */
  257.     if (kbSignal!=-1) {
  258.         FreeSignal(kbSignal);
  259.     }
  260.  
  261.     /* Remove keyboard interrupt handler */
  262.     if (kbIntHandle) {
  263.         RemKBInt(kbIntHandle);
  264.     }
  265.  
  266.     /*
  267.      * Close GUI
  268.      */
  269.  
  270.     /* Close GUI */
  271.     guiClose();
  272.  
  273.     /*
  274.      * Close libraries
  275.      */
  276.  
  277.     /* Close lowlevel.library */
  278.     if (LowLevelBase) {
  279.         CloseLibrary(LowLevelBase);
  280.     }
  281.  
  282.     /* Close keymap.library */
  283.     if (KeymapBase) {
  284.         CloseLibrary(KeymapBase);
  285.     }
  286.  
  287.     /* Close gadtools.library */
  288.     if (GadtoolsBase) {
  289.         CloseLibrary(GadtoolsBase);
  290.     }
  291.  
  292.     /* Close intuition.library */
  293.     if (IntuitionBase) {
  294.         CloseLibrary(IntuitionBase);
  295.     }
  296.  
  297.     /*
  298.      * Exit
  299.      */
  300.  
  301.     /* Exit */
  302.     exit(returnCode);
  303.  
  304. }
  305.  
  306.